//
//  UIImageView + Extension.swift
//  微博新浪
//
//  Created by hezerong on 16/6/8.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit
import SDWebImage

extension UIImageView{


    //1. 便利构造器面前要使用convenience修饰
    convenience init (imageName:String){
    
    //2. 便利构造器总是调用自己的便利构造器来完成构造过程
        self.init()
        
        let image = UIImage(named: imageName)
        
        self.image = image
    
    }
    
    
    //封装异步加载图片方法
    func setImage(urlString:String?,placeHolder:String){
    
    
        guard let urlstring = urlString , url = NSURL(string: urlstring) else {
        
            return
        }
        
        self.sd_setImageWithURL(url, placeholderImage: UIImage(named: placeHolder))
        
//        self.sd_setImageWithURL(url, placeholderImage: UIImage(named: placeHolder)) { (downLoadImage, _, _, _) -> Void in
//            
//            let circleImage = downLoadImage.createCircleImage(true, size: CGSizeMake(36, 36), color: UIColor.whiteColor())
//            
//            self.image = circleImage
//        }
        
        
    
           }
    
    func setCircleImage(urlString:String?,placeHolder:String,iscornered:Bool=false,size:CGSize,color:UIColor = UIColor.whiteColor()){
    
        guard let urlstring = urlString , url = NSURL(string: urlstring) else {
            
            return
        }
        
        SDWebImageManager.sharedManager().downloadImageWithURL(url, options: [], progress: nil) { (downloadImage, _, _, _, _) -> Void in
            
            downloadImage.createCircleImage(iscornered, size: size, color: color)
            downloadImage.async_createCircleImgae(iscornered, size: size, color: color, completion: { (circleImage) -> () in
                
                self.image = circleImage
            })
        }

       
    }


}
